/*
 * Copyright 2016 SoChishun.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v.main;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import m.RuleModels;
import util.JFrameUtil;

/**
 * 主菜单加载器
 * @author SoChishun
 * @version 1.0 2016-5-25
 */
public class MenuBarLoader implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        String[] acommand = e.getActionCommand().split("\\|");
        JFrameUtil.openJFrame(acommand[0], acommand[1]);
    }

    /**
     * 加载菜单
     *
     * @since 1.0 2016-5-20
     */
    public void loadMenu(JMenuBar menubar_main) {
        menubar_main.removeAll(); // 清空所有菜单
        JMenu menu;
        JMenuItem menuItem;
        RuleModels ruleModels = new RuleModels();
        List<HashMap> list1, list2;
        list1 = ruleModels.getRules();
        for (HashMap node1 : list1) {
            try {
                menu = getMenu(JMenu.class, node1);
                list2 = (List<HashMap>) node1.get("children");
                for (HashMap node2 : list2) {
                    menuItem = getMenu(JMenuItem.class, node2);
                    menu.add(menuItem);
                }
                menubar_main.add(menu);
            } catch (InstantiationException ex) {
                Logger.getLogger(MenuBarLoader.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MenuBarLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * 生成菜单项
     * @param <T>
     * @param c
     * @param rule
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException 
     * @since 1.0 2016-5-24 SoChishun Added.
     */
    <T extends JMenuItem> T getMenu(Class<T> c, HashMap rule) throws InstantiationException, IllegalAccessException {
        String text = rule.get("text").toString();
        String class_name = rule.get("class_name").toString();
        String name = rule.get("name").toString();
        T menu = c.newInstance();
        menu.setText(text);
        if (name.length() > 0) {
            menu.setMnemonic(name.charAt(0));
        }
        if (class_name.length() > 1) {
            menu.setActionCommand(class_name + "|" + text);
            menu.addActionListener(this);
        }
        return menu;
    }
}
